package com.googlecode.aleverr.yafotkiclient.api;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import org.w3c.dom.Document;
import org.w3c.dom.Node;

import javax.xml.xpath.*;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.net.URL;

public class Photo implements Serializable {

    private static final String TAG = "Photo";

    private static final XPathExpression TITLE_EXPRESSION;
    private static final XPathExpression XS_IMG_EXPRESSION;
    private static final XPathExpression M_IMG_EXPRESSION;

    static {
        XPath xPath = XPathFactory.newInstance().newXPath();
        XPathExpression expression = null;
        try {
            expression = xPath.compile("title/text()");
        } catch (XPathExpressionException e) {
            e.printStackTrace();
        } finally {
            TITLE_EXPRESSION = expression;
        }
        expression = null;
        try {
            expression = xPath.compile("img[@size='XS']/@href");
        } catch (XPathExpressionException e) {
            e.printStackTrace();
        } finally {
            XS_IMG_EXPRESSION = expression;
        }
        expression = null;
        try {
            expression = xPath.compile("img[@size='M']/@href");
        } catch (XPathExpressionException e) {
            e.printStackTrace();
        } finally {
            M_IMG_EXPRESSION = expression;
        }
    }

    private String title;
    private String xsAddress;
    private String mAddress;

    private Photo() {
    }

    public static Photo fromUrl(String address, String accessToken) {
        Document document = Utils.obtainDocument(address, accessToken);
        if (document != null) {
            return fromEntryNode(document.getFirstChild());
        }
        return null;
    }

    public static Photo fromEntryNode(Node entryNode) {
        Photo result = new Photo();
        try {
            result.title = (String) TITLE_EXPRESSION.evaluate(entryNode, XPathConstants.STRING);
        } catch (XPathExpressionException e) {
            Log.e(TAG, e.getMessage(), e);
        }
        try {
            result.xsAddress = (String) XS_IMG_EXPRESSION.evaluate(entryNode, XPathConstants.STRING);
        } catch (XPathExpressionException e) {
            Log.e(TAG, e.getMessage(), e);
        }
        try {
            result.mAddress = (String) M_IMG_EXPRESSION.evaluate(entryNode, XPathConstants.STRING);
        } catch (XPathExpressionException e) {
            Log.e(TAG, e.getMessage(), e);
        }
        return result;
    }

    public static Photo uploadPhoto(String address, String accessToken, FormDataGenerator generator) {
        Document document = Utils.obtainDocument(address, accessToken, generator);
        if (document != null) {
            return fromEntryNode(document.getFirstChild());
        }
        return null;
    }

    private Bitmap getBitmap(String address) {
        try {
            InputStream in = new URL(address).openStream();
            return BitmapFactory.decodeStream(in);
        } catch (IOException e) {
            Log.e(TAG, e.getMessage(), e);
        }
        return null;
    }

    public String getTitle() {
        return title;
    }

    public Bitmap getXSBitmap() {
        return getBitmap(xsAddress);
    }

    public Bitmap getMBitmap() {
        return getBitmap(mAddress);
    }
}
